function removeFloatmap()
{  
  var mapName = getTextFromList("TextureManager_FloatmapList"); 
  if(confirm("Are you sure want to delete '"+mapName+"' float-map?")) 
  {
    this.game.resourceManager.removeFloatmap(mapName);
    updateFloatmapList("TextureManager_FloatmapList");
  }
}
function addFloatmap()
{
  var name  = document.getElementById("addFloatmapDialog_Name").value;
  var url = document.getElementById("addFloatmapDialog_URL").value;
  this.game.resourceManager.load(name,url,true,false);  
  updateFloatmapList("TextureManager_FloatmapList");
  $("#addFloatmapDialog").dialog("close");
}
function updateFloatmapList(list_id,name)
{
  var floatmapList = this.game.resourceManager.floatMaps;
  var temp = [""];
  for (var map in floatmapList) {
    temp.push(map);
  }
  temp.sort();
  
  var dropList = document.getElementById(list_id);
  // Remove all old entries
  dropList.options.length = 0
  // Update new entries
  for (var i = 0; i < temp.length; i++) {
    var op = document.createElement('option');
    op.text = temp[i];
    dropList.add(op, null);
    
    if (name && name == temp[i]) 
      dropList.selectedIndex = i;
  }
}
function modTexture()
{
  var magFilterId = document.getElementById("modTextureDialog_MagFilter").selectedIndex;
  var magFilter = 0;
  switch(magFilterId)
  {
    case 0:
      magFilter = gl.LINEAR;break;
    case 1:
      magFilter = gl.NEAREST;break;
  }
  
  var minFilterId = document.getElementById("modTextureDialog_MinFilter").selectedIndex;
  var minFilter = 0;
  switch(minFilterId)
  {
    case 0:
      minFilter = gl.LINEAR_MIPMAP_LINEAR;break;
    case 1:
      minFilter = gl.LINEAR;break;
    case 2:
      minFilter = gl.NEAREST_MIPMAP_NEAREST;break;
    case 3:
      minFilter = gl.LINEAR_MIPMAP_NEAREST;break;
    case 4:
      minFilter = gl.NEAREST_MIPMAP_LINEAR;break;
    case 5:
      minFilter = gl.NEAREST;break;  
  }
  
  var wrapS_Id = document.getElementById("modTextureDialog_WrapS").selectedIndex;
  var wrapS = 0;
  switch(wrapS_Id)
  {
    case 0:
      wrapS = gl.REPEAT;break;
    case 1:
      wrapS = gl.CLAMP_TO_EDGE;break;
    case 2:
      wrapS = gl.MIRRORED_REPEAT;break;
  }
  
  var wrapT_Id = document.getElementById("modTextureDialog_WrapT").selectedIndex;
  var wrapT = 0;
  switch(wrapT_Id)
  {
    case 0:
      wrapT = gl.REPEAT;break;
    case 1:
      wrapT = gl.CLAMP_TO_EDGE;break;
    case 2:
      wrapT = gl.MIRRORED_REPEAT;break;
  }
  var screen = this.game.getScreen(TEXTURE_MANAGER_SCREEN_ID);
  var texParam = new gewTextureParameter(magFilter,minFilter,wrapS,wrapT);
  screen.activeTexture.setTextureParam(texParam);
  $("#modTextureDialog").dialog("close");
  displayTexture();
}
function removeTexture()
{
  var list = document.getElementById("TextureManager_TextureList");
  var selectId = list.selectedIndex;
  var op = list.options[selectId];
  var texName = op.text; 
  if(confirm("Are you sure want to delete '"+texName+"' texture?")) 
  {
    this.game.resourceManager.removeTexture(texName);
    updateTextureList();
  }
}
function displayTextureByGLName()
{
  var screen = this.game.getScreen(TEXTURE_MANAGER_SCREEN_ID);
  screen.setActiveGLTexture( getTextFromInput("TextureManager_GLName"));
}
function displayTexture()
{
  var list = document.getElementById("TextureManager_TextureList");
  var selectId = list.selectedIndex;
  var op = list.options[selectId];
  if(!op)
    return;
  var texName = op.text;
  var screen = this.game.getScreen(TEXTURE_MANAGER_SCREEN_ID);
  screen.setActiveTexture(texName);   
  var tex = this.game.resourceManager.getTexture(texName);
  
  var urlText = document.getElementById("TextureManager_URL");
  urlText.textContent = "URL:"+tex.url;
  var widthText = document.getElementById("TextureManager_Width");
  widthText.textContent = "Width:"+tex.width;
  
  var widthText = document.getElementById("TextureManager_Width");
  widthText.textContent = "Width:"+tex.width;
  
  var heightText = document.getElementById("TextureManager_Height");
  heightText.textContent = "Height:"+tex.height;
  
  var format = tex.format;
  var format_str;
  switch(format)
  {
    case gl.RGB:
      format_str = "RGB";break;
    case gl.RGBA:
      format_str = "RGBA";break;
    case gl.ALPHA:
      format_str = "ALPHA";break;
    case gl.LUMINANCE:
      format_str = "LUMINANCE";break;
    case gl.LUMINANCE_ALPHA:
      format_str = "LUMINANCE_ALPHA";break;
  }
  var formatText = document.getElementById("TextureManager_Format");
  formatText.textContent = "Format:"+format_str;
  
  gl.bindTexture(gl.TEXTURE_2D,tex.glName);
  var magFilter = gl.getTexParameter(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER);
  var minFilter = gl.getTexParameter(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER);
  var wrapS = gl.getTexParameter(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S);
  var wrapT = gl.getTexParameter(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T);
  
  var temp = null;
  var magText = document.getElementById("TextureManager_MagFilter");
  switch(magFilter)
  {
    case gl.LINEAR:
      temp = "LINEAR";break;
    case gl.NEAREST:
      temp = "NEAREST";break;
  }
  magText.textContent = "magFilter:"+temp;
  
  var minText = document.getElementById("TextureManager_MinFilter");
  switch(minFilter)
  {
    case gl.LINEAR:
      temp = "LINEAR";break;
    case gl.NEAREST:
      temp = "NEAREST";break;
    case gl.LINEAR_MIPMAP_LINEAR:
      temp = "LINEAR_MIPMAP_LINEAR";break;
    case gl.NEAREST_MIPMAP_NEAREST:
      temp = "NEAREST_MIPMAP_NEAREST";break;
    case gl.LINEAR_MIPMAP_NEAREST:
      temp = "LINEAR_MIPMAP_NEAREST";break;
    case gl.NEAREST_MIPMAP_LINEAR:
      temp = "NEAREST_MIPMAP_LINEAR";break;
  }
  minText.textContent = "minFilter:"+temp;
  
  var wrapSText = document.getElementById("TextureManager_WrapS");
  switch(wrapS)
  {
    case gl.REPEAT:
      temp = "REPEAT";break;
    case gl.CLAMP_TO_EDGE:
      temp = "CLAMP_TO_EDGE";break;
    case gl.MIRRORED_REPEAT:
      temp = "MIRRORED_REPEAT";break;
  }
  wrapSText.textContent = "wrapS:"+temp;
  
  var wrapTText = document.getElementById("TextureManager_WrapT");
  switch(wrapT)
  {
    case gl.REPEAT:
      temp = "REPEAT";break;
    case gl.CLAMP_TO_EDGE:
      temp = "CLAMP_TO_EDGE";break;
    case gl.MIRRORED_REPEAT:
      temp = "MIRRORED_REPEAT";break;
  }
  wrapTText.textContent = "wrapT:"+temp;
  
  var mipmapText = document.getElementById("TextureManager_IsMipmaped");
  mipmapText.textContent = "IsMipmaped:"+tex.isMipmaped;
  var flipYText = document.getElementById("TextureManager_IsFlipY");
  flipYText.textContent = "IsFlipY:"+tex.isFlipY;
  
  var preAlphaText = document.getElementById("TextureManager_IsPremultiedAlpha");
  preAlphaText.textContent = "IsPremultipliedAlpha:"+tex.isPremultipliedAlpha;
}
function addTexture()
{
  var name  = document.getElementById("addTextureDialog_Name").value;
  var url = document.getElementById("addTextureDialog_URL").value;
  
  var formatIdx = document.getElementById("addTextureDialog_Format").selectedIndex;
  var format;
  switch(formatIdx)
  {
    case 0:
      format = gl.RGB;break;
    case 1:
      format = gl.RGBA;break;
    case 2:
      format = gl.ALPHA;break;
    case 3:
      format = gl.LUMINANCE;break;
    case 4:
      format = gl.LUMINANCE_ALPHA;break;
  }
  var magFilterId = document.getElementById("addTextureDialog_MagFilter").selectedIndex;
  var magFilter = 0;
  switch(magFilterId)
  {
    case 0:
      magFilter = gl.LINEAR;break;
    case 1:
      magFilter = gl.NEAREST;break;
  }
  
  var minFilterId = document.getElementById("addTextureDialog_MinFilter").selectedIndex;
  var minFilter = 0;
  switch(minFilterId)
  {
    case 0:
      minFilter = gl.LINEAR_MIPMAP_LINEAR;break;
    case 1:
      minFilter = gl.LINEAR;break;
    case 2:
      minFilter = gl.NEAREST_MIPMAP_NEAREST;break;
    case 3:
      minFilter = gl.LINEAR_MIPMAP_NEAREST;break;
    case 4:
      minFilter = gl.NEAREST_MIPMAP_LINEAR;break;
    case 5:
      minFilter = gl.NEAREST;break;  
  }
  
  var wrapS_Id = document.getElementById("addTextureDialog_WrapS").selectedIndex;
  var wrapS = 0;
  switch(wrapS_Id)
  {
    case 0:
      wrapS = gl.REPEAT;break;
    case 1:
      wrapS = gl.CLAMP_TO_EDGE;break;
    case 2:
      wrapS = gl.MIRRORED_REPEAT;break;
  }
  
  var wrapT_Id = document.getElementById("addTextureDialog_WrapT").selectedIndex;
  var wrapT = 0;
  switch(wrapT_Id)
  {
    case 0:
      wrapT = gl.REPEAT;break;
    case 1:
      wrapT = gl.CLAMP_TO_EDGE;break;
    case 2:
      wrapT = gl.MIRRORED_REPEAT;break;
  }
  
  var isGenMipmap = document.getElementById("addTextureDialog_IsGenerateMipmap").checked;
  var isFlipY = document.getElementById("addTextureDialog_IsFlipY").checked;
  var isPremultiedAlpha = document.getElementById("addTextureDialog_IsPremultiedAlpha").checked;
  var texParam = new gewTextureParameter(magFilter,minFilter,wrapS,wrapT);
  this.game.resourceManager.load(name,url,true,false,format,texParam,isGenMipmap,isFlipY,isPremultiedAlpha);
  updateTextureList(name);
  $("#addTextureDialog").dialog("close");
  list = document.getElementById("TextureManager_TextureList");
  setTimeout(function(){displayTexture()},500);
}

// List of all ui_texture_select_element need to be update
var UI_Texture_Element_Ids = [
  "TextureManager_TextureList",
  "addSkyboxEffectDialog_TextureList",
  "MeshManager_DisplayTextureList",
];
function updateTextureList(name)
{  
  var textureList = this.game.resourceManager.textureManager.textureList;
  var temp = [""];
  for (var tex in textureList) {
    temp.push(tex);
  }
  temp.sort();
    
  var elementNum = UI_Texture_Element_Ids.length;
  for (var ele = 0; ele < elementNum; ele++) 
  {
    var dropList = document.getElementById(UI_Texture_Element_Ids[ele]);
    // Remove all old entries
    dropList.options.length = 0
    // Update new entries
    for (var i = 0; i < temp.length; i++) {
      var op = document.createElement('option');
      op.text = temp[i];
      dropList.add(op, null);
      if (name && name == temp[i]) 
        dropList.selectedIndex = i;
    }
  }
}
function updateTextureListInto(list,name)
{  
  var textureList = this.game.resourceManager.textureManager.textureList;
  var temp = [""];
  for (var tex in textureList) {
    temp.push(tex);
  }
  temp.sort();
  
  var dropList = document.getElementById(list);
  // Remove all old entries
  dropList.options.length = 0
  // Update new entries
  for (var i = 0; i < temp.length; i++) {
    var op = document.createElement('option');
    op.text = temp[i];
    dropList.add(op, null);
    if (name && name == temp[i]) 
      dropList.selectedIndex = i;
  }
}

function TextureManagerScreen(game)
{
  gewScreen.call(this, game);
  this.scene2d = new gewScene2D(game);
  this.activeTexture = null;
  
  var s = this.sprite = new gewSprite2D();
  s.setAsRectangle(512,512);
  s.setPosition(0,0);
  this.subTexture = new gewSubTexture();
  this.subTexture.woffset = 0;
  this.subTexture.hoffset = 1;
  this.subTexture.width = 1.;
  this.subTexture.height = -1.;
  s.subTexture = this.subTexture;
  this.scene2d.atlas = new gewTextureAtlas();
  this.scene2d.atlas.isReady = true;
  this.scene2d.layer0.insert(s);
  this.setActiveGLTexture = function(glName)
  {
    var tex = new gewTexture(this.gl,this.gl.TEXTURE_2D);
    tex.glName = eval(glName);
    tex.width = 2048;
    tex.height = 2048;
    tex.isReady = true;
    this.activeTexture = tex;
    this.subTexture.mainTexture = tex;
    this.sprite.isUpdateTexcoord = true;
    //this.sprite.setSubRegion(0,0,tex.width,tex.height);
    this.scene2d.atlas.textureList[0] = tex;
  }
  this.setActiveTexture = function(texName)
  {
    var tex = this.game.resourceManager.getTexture(texName);
    this.activeTexture = tex;
    this.subTexture.mainTexture = tex;
    this.sprite.isUpdateTexcoord = true;
    //this.sprite.setSubRegion(0,0,tex.width,tex.height);
    this.scene2d.atlas.textureList[0] = tex;
  }
  this.onTick = function(mls)
  {
    var gl = this.game.gl;
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    if(this.activeTexture)
      this.scene2d.render();
  }
  this.onMouseDown = function(e)
  {
    return false;
  }
  this.onKeyDown = function(e)
  {
    return false;
  }
  this.onKeyUp = function(e)
  {
    return false;
  }
  this.onMouseMove = function(e)
  {    
    return false;
  }
  this.onMouseUp = function(e)
  {
    return false;
  }
  this.onMouseWheel = function(e)
  {
    return false;    
  }
}